/*	kd:
	
	The shop gets instantiated here and you also control it from here. This is
	also the place to ensure the shops get propagated to everything that needs
	them (since we wanna be able to conveniently read them in other event
	handlers that do other things).
	
	*/

class Gc_Shop_Entry : EventHandler {
	const active_inv = "MenuWheelActive";
	
	Gc_Shop m_shop;
	bool     m_shop_is_open;
	
	// Give the player an instance of the script.
	override void PlayerEntered (PlayerEvent event) {
		
		// Set up the shop for the client machine.
		if(event.playernumber == consoleplayer) {
			let base_data = Gcs_BaseShop.Find();
			
			if(base_data == NULL) {
				Console.printf("Note: No GC default shops available. "
					"Couldn't find a Gcs_BaseShop thinker instantiated or something.");
				return;
			}
			
			m_shop = new('Gc_Shop');
			// m_shop.OnBuild();
			m_shop.Init(players [event.playernumber]);
			// m_shop.SetShopList(base_data.m_shops);
			m_shop_is_open = false;
			// m_shop.TriggerShopOpen();
		}
		
		// Unfreeze us if we enter a map with a shop wheel item. It isn't
		// exactly the best tracking, but shop shenanigans allow you to bypass
		// a lot of dumb stuff anyway, so...
		
		let mo = players [event.playernumber].mo;
		
		if(mo && 0 < mo.CountInv(active_inv)) {
			mo.TakeInventory(active_inv, 0x7fffffff);
			mo.player.cheats &= ~(CF_FROZEN | CF_TOTALLYFROZEN);
		}
	}
	
	// kd: Set the GC shop as default for now.
	
	override void NewGame () {
		Gcs_BaseShop base_shop = new("Gcs_BaseShop");
		base_shop.PostBeginPlay();
	}
	
	override void WorldLoaded (WorldEvent event) {
		SetupPlayerShopData();
		InitPlayerGlowTables();
	}
	
	override void WorldTick () {
		if(m_shop && m_shop.m_is_active) {
			m_shop.TickShopControl();
		}
	}
	
	override void PlayerDisconnected (PlayerEvent event) {
		let handler = Gcs_PlayerData(Gcs_PlayerData.Find("Gcs_PlayerData"));
		
		if(handler) {
			handler.RemoveShopkeeper(event.playernumber);
		}
	}
	
	override void RenderOverlay (RenderEvent event) {
		if(m_shop && m_shop.m_is_active) {
			m_shop.DrawEverything(event);
		}
	}
	
	override void NetworkProcess (ConsoleEvent event) {
		String cmd  = event.name;
		int p       = event.player;
		
		if(p < 0 || !players [p].mo) {
			return;
		}
		
		if(cmd ~== "GimboStratus") {
			let base_data = Gcs_BaseShop.Find();
			let play_data = Gcs_PlayerData(Gcs_PlayerData.Find("Gcs_PlayerData"));
			
			if(base_data && play_data) {
				ToggleShop(p, base_data.ShopList());
			}
		}
	}
	
	// kd: Whoever implemented no freelook by setting ALL pitch inputs = 0 (even
	// the supposedly "original input") can go swallow a watermelon sideways.
	
	override bool InputProcess (InputEvent event) {
		m_shop.HandleMouseMove(event.mousex, event.mousey);
		return false;
	}
	
	void ToggleShop (uint player, Gcs_ShopList shop_list) {
		if(MAXPLAYERS <= player || shop_list == NULL) {
			return;
		}
		
		let mo = players [player].mo;
		
		// Close the shop:
		if(0 < mo.CountInv(active_inv)) {
			CloseShop(player);
		}
		
		// Open it up:
		else if(0 < mo.health) {
			OpenShop(player, shop_list);
		}
	}
	
	protected void OpenShop (uint player, Gcs_ShopList shop_list) {
		if(maxplayers <= player) {
			return;
		}
		
		if(!Guncaster(players [player].mo)) {
			if(consoleplayer == player) {
				Console.printf("Wait! You can't let the shopkeeper see you "
					"disgraced like this!");
			}
			
			return;
		}
		
		Actor mo = players [player].mo;
		
		if(mo) {
			mo.GiveInventory(active_inv, 1);
			mo.GiveInventory("NoTiltZone", 1);
			
			if(player == consoleplayer) {
				m_shop.TriggerShopOpen(shop_list);
				m_shop_is_open = true;
			}
		}
	}
	
	protected void CloseShop (uint player) {
		Actor mo = players [player].mo;
		
		if(mo) {
			mo.TakeInventory(active_inv, 0x7fffffff);
			mo.TakeInventory("NoTiltZone", 0x7fffffff);
			
			if(player == consoleplayer) {
				m_shop.TriggerShopClose();
				m_shop_is_open = false;
			}
		}
	}
	
	static void StaticShopOff (uint player) {
		if(maxplayers <= player || !playeringame [player]) {
			return;
		}
		
		let handler = Gcs_ShopEntry(Find("Gcs_ShopEntry"));
		
		if(handler) {
			handler.CloseShop(player);
		}
	}
	
	// move to playerentered lol
	/*protected void PlayerSetupFix () const {
		for(int i = 0; i < maxplayers; i++)
		if(playeringame [i]) {
			Guncaster mo = Guncaster(players [i].mo);
			mo.GiveInventory("IHoldPlayerVariables", 1); // Storage for people who die.
			
			// Let's cache this whenever setup, respawn, whatever is done.
			mo.player_data = IholdPlayerVariables(mo.FindInventory("IholdPlayerVariables"));
			
			if(mo.player_data) {
				mo.player_data.FullReset();
			}
		}
	} */
	
	protected void SetupPlayerShopData () const {
		let base_data = Gcs_BaseShop.Find();
		let play_data = Gcs_PlayerData(Gcs_PlayerData.Find("Gcs_PlayerData"));
		
		if(!base_data || !play_data) {
			Console.printf("\c[ice]Gcs_ShopEntry::SetupPlayerShopData: "
				"\c-base_data is %s. play_data is %s",
				base_data ? "Init OK" : "NULL",
				play_data ? "Init OK" : "NULL");
			return;
		}
		
		for(int i = 0; i < maxplayers; i++)
		if(playeringame [i]) {
			play_data.SetShopList(i, base_data.ShopList());
		}
	}
	
	protected void InitPlayerGlowTables () const {
		let base_data = Gcs_BaseShop.Find();
		let play_data = Gcs_PlayerData(Gcs_PlayerData.Find("Gcs_PlayerData"));
		
		if(!base_data || !play_data) {
			Console.printf("\c[ice]Gcs_ShopEntry::SetupPlayerShopData: "
				"\c-base_data is %s. play_data is %s",
				base_data ? "Init OK" : "NULL",
				play_data ? "Init OK" : "NULL");
			return;
		}
		
		for(int i = 0; i < maxplayers; i++)
		if(playeringame [i]) {
			Guncaster mo = Guncaster(players [i].mo);
			
			if(mo) {
				mo.InitGlowTable(base_data.ShopList());
			}
		}
	}
}